package com.gimi.cloud.bbp.manage.service.order.impl;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.MyBeanUtils;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.dao.dao.order.OrderExpressDao;
import com.gimi.cloud.bbp.dto.order.OrderExpressDTO;
import com.gimi.cloud.bbp.entity.order.OrderExpressEntity;
import com.gimi.cloud.bbp.manage.service.order.OrderExpressService;

import tk.mybatis.mapper.entity.Example;
@Service
@Transactional
public class OrderExpressServiceImpl implements OrderExpressService {
	
	@Autowired
	private OrderExpressDao orderExpressDao;

	@Override
	public List<OrderExpressEntity> orderExpressByCondition(OrderExpressDTO DTO) throws Exception {
        Example example = new Example(OrderExpressEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andCondition("is_delete =", "0");
        //订单id
        if(StringUtil.isBlank(DTO.getoId())){
        	return null;
        }
        criteria.andCondition("o_id = ", DTO.getoId());
        List<OrderExpressEntity> list = orderExpressDao.selectByExample(example);
        return list;
	}

	@Override
	public void deleteOrderExpress(OrderExpressDTO DTO) throws Exception {
		OrderExpressEntity entity = orderExpressDao.selectByPrimaryKey(DTO.getOeId());
        if (entity != null) {
            //删除订单发票信息
            entity.setIsDelete(1);//设置删除属性
            int i = orderExpressDao.updateByPrimaryKey(entity);
            if (i <= 0 ) {
				throw new MsgException(ResponseCodeEnum.FAILURE, "订单配送信息删除失败!");
			}
        }
	}

	@Override
	public OrderExpressDTO getSingleOrderExpress(OrderExpressDTO DTO)throws Exception {
		OrderExpressEntity entity = orderExpressDao.selectByPrimaryKey(DTO.getOeId());
		OrderExpressDTO dto = new OrderExpressDTO();
        MyBeanUtils.copyProperties(entity,dto);
        return dto;
	}

	@Override
	public void updateOrderExpress(OrderExpressDTO DTO) throws Exception {
		OrderExpressEntity entity = orderExpressDao.selectByPrimaryKey(DTO.getOeId());
		MyBeanUtils.copyPropertiesIgnoreNull(DTO,entity);
        entity.setUpdateTime(new Date());
        int i = orderExpressDao.updateByPrimaryKey(entity);
        if (i <= 0 ) {
        	throw new MsgException(ResponseCodeEnum.FAILURE, "更新订单配送信息失败!");
        }
	}
	
	
	
	
	
	
	
	
	

}
